﻿using System;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.web.Controls.Article;
using eCommerce.web.Controls.Sidebar;
using eCommerce.db.DataRespository;

namespace eCommerce.web.Pages
{
    public partial class ListByType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var name = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
                var parentname = Utils.GetIdFromRequestString(Utils.Parent_Name, string.Empty);
                
                CategoryEntity parent_o = null ; 
                if (!string.IsNullOrEmpty(parentname))
                {
                    name = parentname + "/" + name;
                    Page.RegisterHiddenField("hdCatUrl", parentname);
                    parent_o = CategoryBo.GetByUrl(parentname);
                }
                else
                    Page.RegisterHiddenField("hdCatUrl", name);

                if(name.IndexOf("tin-tuc") !=  -1)
                    Page.RegisterHiddenField("hidCategoryId", "tin-tuc");

                var o = CategoryBo.GetByUrl(name);

                if (name == "san-pham-moi" || name == "khuyen-mai")
                {
                    var listProduct = LoadControl("~/Controls/Product/List.ascx");
                    if (listProduct != null)
                        phRight.Controls.Add(listProduct);
                }
                else if (!string.IsNullOrEmpty(o.Name))
                {
                    var category = LoadControl("~/Controls/Sidebar/Category.ascx");
                    ((Category)category).ParentCategoryId = o.ParentId;
                    ((Category)category).CategoryId   = o.CategoryId;
                    ((Category)category).TypeId       = o.TypeId;
                    ((Category)category).CategoryName = o.Name;

                    if (parent_o != null)
                    {
                        ((Category)category).ParentCategoryName = parent_o.Name;
                    }

                    var testmonials = LoadControl("~/Controls/Sidebar/Testmonials.ascx");
                    if (o.CategoryId == 75)
                        o.TypeId = 1;
                    switch (o.TypeId)
                    {
                        case (int)CategoryType.Product:
                            var listProduct = LoadControl("~/Controls/Product/List.ascx");
                            if (listProduct != null)
                                phRight.Controls.Add(listProduct);
                        
                            if (category != null)
                                phLeft.Controls.Add(category);

                            var listProperty = LoadControl("~/Controls/Property/PropertyByType.ascx");
                            if (listProperty != null)
                                phLeft.Controls.Add(listProperty);

                            if (testmonials != null)
                                phLeft.Controls.Add(testmonials);
                            break;

                        case (int)CategoryType.Article:
                            var listArticle = LoadControl("~/Controls/Article/List.ascx");
                            ((List) listArticle).CategoryId  = o.CategoryId;
                            ((List)listArticle).CategoryName = o.Name;                        
                            if (listArticle != null)
                                phRight.Controls.Add(listArticle);

                            if (category != null)
                                phLeft.Controls.Add(category);

                            var topArticle = LoadControl("~/Controls/Sidebar/TopArticle.ascx");
                            if (topArticle != null)
                                phLeft.Controls.Add(topArticle);

                            var topView = LoadControl("~/Controls/Sidebar/TopView.ascx");
                            if (topView != null)
                                phLeft.Controls.Add(topView);
                                                                            
                            if (testmonials != null)
                                phLeft.Controls.Add(testmonials);
                            break;
                    }
                }
                else switch (name)
                {
                    case "tin-tuc":
                        {
                            var listArticle = LoadControl("~/Controls/Article/List.ascx");
                            ((List)listArticle).CategoryId   = 0;
                            ((List)listArticle).CategoryName = "tin-tuc";
                            if (listArticle != null)
                                phRight.Controls.Add(listArticle);

                            var category = LoadControl("~/Controls/Sidebar/Category.ascx");
                            ((Category)category).CategoryId   = 0;
                            ((Category)category).TypeId       = 0;
                            ((Category)category).CategoryName = "tin-tuc";

                            if (category != null)
                                phLeft.Controls.Add(category);

                            var topArticle = LoadControl("~/Controls/Sidebar/TopArticle.ascx");
                            if (topArticle != null)
                                phLeft.Controls.Add(topArticle);

                            var topView = LoadControl("~/Controls/Sidebar/TopView.ascx");
                            if (topView != null)
                                phLeft.Controls.Add(topView);

                            var testmonials = LoadControl("~/Controls/Sidebar/Testmonials.ascx");
                            if (testmonials != null)
                                phLeft.Controls.Add(testmonials);
                        }
                        break;
                    case "san-pham":
                        break;
                }
            }
        }
    }
}